/**
 * 表情文字转表情图片
 */
function textFace2imgFace(content){
	const patter = /\/:[0-9]{3}/g;
	let strs = content.match(patter);
	if (strs) {
		strs.forEach(str => {
			if (emotions.containsKey(str)) {
				var img = '<img style=\"width:24px;height:24px;\" src=\"static/image/editor/emotion/qq/' + emotions.get(str) + '\"/>';
				content = content.replace(str,img);	
			}
		})
		
	}
	return content;
}
/**
 * 表情图片转表情文字
 */
function imgFace2textFace(content){
	var patt1 = /<img src="static\/image\/editor\/emotion\/qq\/[0-9]{1,3}.gif\">/g;
	var str = content.match(patt1)
	if (str) {
		for (var i = 0; i < str.length ; i++) {
		    var sub = str[i];
			for (var j = 0;j < sub.length - 1 ;j++) {
				var tmp = sub.substring(0, sub.length - j);
				emotions_pic_key=tmp.replace('<img src="static\/image\/editor\/emotion\/qq\/','').replace('">','');
				if (emotions_pic.containsKey(emotions_pic_key)) {
					content = content.replace(tmp,emotions_pic.get(emotions_pic_key));
					break;
				}
			}
		}
	}
	return content;
}



// QQ表情插件
function myMap(){
this.container = new Object();
}


myMap.prototype.put = function(key, value){
this.container[key] = value;
}


myMap.prototype.get = function(key){
return this.container[key];
}


myMap.prototype.keys = function() {
var keyset = new Array();
var count = 0;
for (var key in this.container) {
// 跳过object的extend函数
if (key == 'extend') {
continue;
}
keyset[count] = key;
count++;
}
return keyset;
}


myMap.prototype.size = function() {
var count = 0;
for (var key in this.container) {
// 跳过object的extend函数
if (key == 'extend'){
continue;
}
count++;
}
return count;
}


myMap.prototype.remove = function(key) {
delete this.container[key];
}

myMap.prototype.containsKey = function(key) {
	return this.container[key];
	}

	var emotions = new myMap();
	emotions.put('/:000', '000.gif');
	emotions.put('/:001', '001.gif');
	emotions.put('/:002', '002.gif');
	emotions.put('/:003', '003.gif');
	emotions.put('/:004', '004.gif');
	emotions.put('/:005', '005.gif');
	emotions.put('/:006', '006.gif');
	emotions.put('/:007', '007.gif');
	emotions.put('/:008', '008.gif');
	emotions.put('/:009', '009.gif');
	emotions.put('/:010', '010.gif');
	emotions.put('/:011', '011.gif');
	emotions.put('/:012', '012.gif');
	emotions.put('/:013', '013.gif');
	emotions.put('/:014', '014.gif');
	emotions.put('/:015', '015.gif');
	emotions.put('/:016', '016.gif');
	emotions.put('/:017', '017.gif');
	emotions.put('/:018', '018.gif');
	emotions.put('/:019', '019.gif');
	emotions.put('/:020', '020.gif');
	emotions.put('/:021', '021.gif');
	emotions.put('/:022', '022.gif');
	emotions.put('/:023', '023.gif');
	emotions.put('/:024', '024.gif');
	emotions.put('/:025', '025.gif');
	emotions.put('/:026', '026.gif');
	emotions.put('/:027', '027.gif');
	emotions.put('/:028', '028.gif');
	emotions.put('/:029', '029.gif');
	emotions.put('/:030', '030.gif');
	emotions.put('/:031', '031.gif');
	emotions.put('/:032', '032.gif');
	emotions.put('/:033', '033.gif');
	emotions.put('/:034', '034.gif');
	emotions.put('/:035', '035.gif');
	emotions.put('/:036', '036.gif');
	emotions.put('/:037', '037.gif');
	emotions.put('/:038', '038.gif');
	emotions.put('/:039', '039.gif');
	emotions.put('/:040', '040.gif');
	emotions.put('/:041', '041.gif');
	emotions.put('/:042', '042.gif');
	emotions.put('/:043', '043.gif');
	emotions.put('/:044', '044.gif');
	emotions.put('/:045', '045.gif');
	emotions.put('/:046', '046.gif');
	emotions.put('/:047', '047.gif');
	emotions.put('/:048', '048.gif');
	emotions.put('/:049', '049.gif');
	emotions.put('/:050', '050.gif');
	emotions.put('/:051', '051.gif');
	emotions.put('/:052', '052.gif');
	emotions.put('/:053', '053.gif');
	emotions.put('/:054', '054.gif');
	emotions.put('/:055', '055.gif');
	emotions.put('/:056', '056.gif');
	emotions.put('/:057', '057.gif');
	emotions.put('/:058', '058.gif');
	emotions.put('/:059', '059.gif');
	emotions.put('/:060', '060.gif');
	emotions.put('/:061', '061.gif');
	emotions.put('/:062', '062.gif');
	emotions.put('/:063', '063.gif');
	emotions.put('/:064', '064.gif');
	emotions.put('/:065', '065.gif');
	emotions.put('/:066', '066.gif');
	emotions.put('/:067', '067.gif');
	emotions.put('/:068', '068.gif');
	emotions.put('/:069', '069.gif');
	emotions.put('/:070', '070.gif');
	emotions.put('/:071', '071.gif');
	emotions.put('/:072', '072.gif');
	emotions.put('/:073', '073.gif');
	emotions.put('/:074', '074.gif');
	emotions.put('/:075', '075.gif');
	emotions.put('/:076', '076.gif');
	emotions.put('/:077', '077.gif');
	emotions.put('/:078', '078.gif');
	emotions.put('/:079', '079.gif');
	emotions.put('/:080', '080.gif');
	emotions.put('/:081', '081.gif');
	emotions.put('/:082', '082.gif');
	emotions.put('/:083', '083.gif');
	emotions.put('/:084', '084.gif');
	emotions.put('/:085', '085.gif');
	emotions.put('/:086', '086.gif');
	emotions.put('/:087', '087.gif');
	emotions.put('/:088', '088.gif');
	emotions.put('/:089', '089.gif');
	emotions.put('/:090', '090.gif');
	emotions.put('/:091', '091.gif');
	emotions.put('/:092', '092.gif');
	emotions.put('/:093', '093.gif');
	emotions.put('/:094', '094.gif');
	emotions.put('/:095', '095.gif');
	emotions.put('/:096', '096.gif');
	emotions.put('/:097', '097.gif');
	emotions.put('/:098', '098.gif');
	emotions.put('/:099', '099.gif');
	emotions.put('/:100', '100.gif');
	emotions.put('/:101', '101.gif');
	emotions.put('/:102', '102.gif');
	emotions.put('/:103', '103.gif');
	emotions.put('/:104', '104.gif');
	emotions.put('/:105', '105.gif');
	emotions.put('/:106', '106.gif');
	emotions.put('/:107', '107.gif');
	emotions.put('/:108', '108.gif');
	emotions.put('/:109', '109.gif');
	emotions.put('/:110', '110.gif');
	emotions.put('/:111', '111.gif');
	emotions.put('/:112', '112.gif');
	emotions.put('/:113', '113.gif');
	emotions.put('/:114', '114.gif');
	emotions.put('/:115', '115.gif');
	emotions.put('/:116', '116.gif');
	emotions.put('/:117', '117.gif');
	emotions.put('/:118', '118.gif');
	emotions.put('/:119', '119.gif');
	emotions.put('/:120', '120.gif');
	emotions.put('/:121', '121.gif');
	emotions.put('/:122', '122.gif');
	emotions.put('/:123', '123.gif');
	emotions.put('/:124', '124.gif');
	emotions.put('/:125', '125.gif');
	emotions.put('/:126', '126.gif');
	emotions.put('/:127', '127.gif');
	emotions.put('/:128', '128.gif');
	emotions.put('/:129', '129.gif');
	emotions.put('/:130', '130.gif');
	emotions.put('/:131', '131.gif');
	emotions.put('/:132', '132.gif');
	
	var emotions_pic = new myMap();
	emotions_pic.put('000.gif', '/:000');
	emotions_pic.put('001.gif', '/:001');
	emotions_pic.put('002.gif', '/:002');
	emotions_pic.put('003.gif', '/:003');
	emotions_pic.put('004.gif', '/:004');
	emotions_pic.put('005.gif', '/:005');
	emotions_pic.put('006.gif', '/:006');
	emotions_pic.put('007.gif', '/:007');
	emotions_pic.put('008.gif', '/:008');
	emotions_pic.put('009.gif', '/:009');
	emotions_pic.put('010.gif', '/:010');
	emotions_pic.put('011.gif', '/:011');
	emotions_pic.put('012.gif', '/:012');
	emotions_pic.put('013.gif', '/:013');
	emotions_pic.put('014.gif', '/:014');
	emotions_pic.put('015.gif', '/:015');
	emotions_pic.put('016.gif', '/:016');
	emotions_pic.put('017.gif', '/:017');
	emotions_pic.put('018.gif', '/:018');
	emotions_pic.put('019.gif', '/:019');
	emotions_pic.put('020.gif', '/:020');
	emotions_pic.put('021.gif', '/:021');
	emotions_pic.put('022.gif', '/:022');
	emotions_pic.put('023.gif', '/:023');
	emotions_pic.put('024.gif', '/:024');
	emotions_pic.put('025.gif', '/:025');
	emotions_pic.put('026.gif', '/:026');
	emotions_pic.put('027.gif', '/:027');
	emotions_pic.put('028.gif', '/:028');
	emotions_pic.put('029.gif', '/:029');
	emotions_pic.put('030.gif', '/:030');
	emotions_pic.put('031.gif', '/:031');
	emotions_pic.put('032.gif', '/:032');
	emotions_pic.put('033.gif', '/:033');
	emotions_pic.put('034.gif', '/:034');
	emotions_pic.put('035.gif', '/:035');
	emotions_pic.put('036.gif', '/:036');
	emotions_pic.put('037.gif', '/:037');
	emotions_pic.put('038.gif', '/:038');
	emotions_pic.put('039.gif', '/:039');
	emotions_pic.put('040.gif', '/:040');
	emotions_pic.put('041.gif', '/:041');
	emotions_pic.put('042.gif', '/:042');
	emotions_pic.put('043.gif', '/:043');
	emotions_pic.put('044.gif', '/:044');
	emotions_pic.put('045.gif', '/:045');
	emotions_pic.put('046.gif', '/:046');
	emotions_pic.put('047.gif', '/:047');
	emotions_pic.put('048.gif', '/:048');
	emotions_pic.put('049.gif', '/:049');
	emotions_pic.put('050.gif', '/:050');
	emotions_pic.put('051.gif', '/:051');
	emotions_pic.put('052.gif', '/:052');
	emotions_pic.put('053.gif', '/:053');
	emotions_pic.put('054.gif', '/:054');
	emotions_pic.put('055.gif', '/:055');
	emotions_pic.put('056.gif', '/:056');
	emotions_pic.put('057.gif', '/:057');
	emotions_pic.put('058.gif', '/:058');
	emotions_pic.put('059.gif', '/:059');
	emotions_pic.put('060.gif', '/:060');
	emotions_pic.put('061.gif', '/:061');
	emotions_pic.put('062.gif', '/:062');
	emotions_pic.put('063.gif', '/:063');
	emotions_pic.put('064.gif', '/:064');
	emotions_pic.put('065.gif', '/:065');
	emotions_pic.put('066.gif', '/:066');
	emotions_pic.put('067.gif', '/:067');
	emotions_pic.put('068.gif', '/:068');
	emotions_pic.put('069.gif', '/:069');
	emotions_pic.put('070.gif', '/:070');
	emotions_pic.put('071.gif', '/:071');
	emotions_pic.put('072.gif', '/:072');
	emotions_pic.put('073.gif', '/:073');
	emotions_pic.put('074.gif', '/:074');
	emotions_pic.put('075.gif', '/:075');
	emotions_pic.put('076.gif', '/:076');
	emotions_pic.put('077.gif', '/:077');
	emotions_pic.put('078.gif', '/:078');
	emotions_pic.put('079.gif', '/:079');
	emotions_pic.put('080.gif', '/:080');
	emotions_pic.put('081.gif', '/:081');
	emotions_pic.put('082.gif', '/:082');
	emotions_pic.put('083.gif', '/:083');
	emotions_pic.put('084.gif', '/:084');
	emotions_pic.put('085.gif', '/:085');
	emotions_pic.put('086.gif', '/:086');
	emotions_pic.put('087.gif', '/:087');
	emotions_pic.put('088.gif', '/:088');
	emotions_pic.put('089.gif', '/:089');
	emotions_pic.put('090.gif', '/:090');
	emotions_pic.put('091.gif', '/:091');
	emotions_pic.put('092.gif', '/:092');
	emotions_pic.put('093.gif', '/:093');
	emotions_pic.put('094.gif', '/:094');
	emotions_pic.put('095.gif', '/:095');
	emotions_pic.put('096.gif', '/:096');
	emotions_pic.put('097.gif', '/:097');
	emotions_pic.put('098.gif', '/:098');
	emotions_pic.put('099.gif', '/:099');
	emotions_pic.put('100.gif', '/:100');
	emotions_pic.put('101.gif', '/:101');
	emotions_pic.put('102.gif', '/:102');
	emotions_pic.put('103.gif', '/:103');
	emotions_pic.put('104.gif', '/:104');
	emotions_pic.put('105.gif', '/:105');
	emotions_pic.put('106.gif', '/:106');
	emotions_pic.put('107.gif', '/:107');
	emotions_pic.put('108.gif', '/:108');
	emotions_pic.put('109.gif', '/:109');
	emotions_pic.put('110.gif', '/:110');
	emotions_pic.put('111.gif', '/:111');
	emotions_pic.put('112.gif', '/:112');
	emotions_pic.put('113.gif', '/:113');
	emotions_pic.put('114.gif', '/:114');
	emotions_pic.put('115.gif', '/:115');
	emotions_pic.put('116.gif', '/:116');
	emotions_pic.put('117.gif', '/:117');
	emotions_pic.put('118.gif', '/:118');
	emotions_pic.put('119.gif', '/:119');
	emotions_pic.put('120.gif', '/:120');
	emotions_pic.put('121.gif', '/:121');
	emotions_pic.put('122.gif', '/:122');
	emotions_pic.put('123.gif', '/:123');
	emotions_pic.put('124.gif', '/:124');
	emotions_pic.put('125.gif', '/:125');
	emotions_pic.put('126.gif', '/:126');
	emotions_pic.put('127.gif', '/:127');
	emotions_pic.put('128.gif', '/:128');
	emotions_pic.put('129.gif', '/:129');
	emotions_pic.put('130.gif', '/:130');
	emotions_pic.put('131.gif', '/:131');
	emotions_pic.put('132.gif', '/:132');

(function($){
	$.fn.qqFace = function(options){
		var defaults = {
			id : 'facebox',
			path : 'face/',
			assign : 'content',
			tip : 'em_'
		};
		var option = $.extend(defaults, options);
		var assign = $('#'+option.assign);
		var id = option.id;
		var path = option.path;
		var tip = option.tip;

		if(assign.length<=0){
			alert('缺少表情赋值对象。');
			return false;
		}

		$(this).click(function(e){
			var strFace, labFace, myMapValue;
			if($('#'+id).length<=0){
				strFace = '<div id="'+id+'" style="position:absolute;display:none;z-index:99999999;" class="qqFace">' +
							  '<table border="0" cellspacing="0" cellpadding="0"><tr>';
				var keys = emotions.keys();
				var num = 0;
				for(var i=0; i<keys.length; i++){
					labFace = keys[i];
					myMapValue = emotions.get(labFace);
					strFace += '<td><img src="'+path+myMapValue+'" data-face="' + labFace + '" onclick="$(\'div.layui-show #'+option.assign+'\').setCaret();$(\'div.layui-show #'+option.assign+'\').insertAtCaret($(this).data(\'face\'));" /></td>';
					num = i+1;
					if(num % 15 == 0 ){
						strFace += '</tr><tr>';
					}
				}
				strFace += '</tr></table></div>';
			}
			/*var selectFace = function(assign,labFace){
				$('#'+assign).setCaret();$('#'+assign).insertAtCaret(labFace);
			}*/
			//console.info(strFace);
			$("body").append(strFace);
			var offset = $(this).position();
			//console.info($(this));
			var top = offset.top + $(this).outerHeight();
			//console.info($("#layui-layim-chat > div > div.layim-chat.layim-chat-friend.layui-show > div.layim-chat-main").height()+80+parseInt($("#layui-layer3").css("top").replace("px")));
			$('#'+id).css('top',$(".layim-chat-main").height()+$(".layer-anim").position().top+$(document).scrollTop());
			$('#'+id).css('left',$(".layer-anim").position().left);//+parseInt($(".layer-anim").css("left").replace("px"))
			$('#'+id).show();
			e.stopPropagation();
		});

		$(document).click(function(){
			$('#'+id).hide();
			$('#'+id).remove();
		});
	};

})(jQuery);

jQuery.extend({
unselectContents: function(){
	if(window.getSelection)
		window.getSelection().removeAllRanges();
	else if(document.selection)
		document.selection.empty();
	}
});
jQuery.fn.extend({
	selectContents: function(){
		$(this).each(function(i){
			var node = this;
			var selection, range, doc, win;
			if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined'){
				range = doc.createRange();
				range.selectNode(node);
				if(i == 0){
					selection.removeAllRanges();
				}
				selection.addRange(range);
			} else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())){
				range.moveToElementText(node);
				range.select();
			}
		});
	},

	setCaret: function(){
		return;
		var initSetCaret = function(){
			var textObj = $(this).get(0);
			textObj.caretPos = document.selection.createRange().duplicate();
		};
		$(this).click(initSetCaret).select(initSetCaret).keyup(initSetCaret);
	},

	insertAtCaret: function(textFeildValue){
		var textObj = $(this).get(0);
		if(document.all && textObj.createTextRange && textObj.caretPos){
			var caretPos=textObj.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == '' ?
			textFeildValue+'' : textFeildValue;
		} else if(textObj.setSelectionRange){
			var rangeStart=textObj.selectionStart;
			var rangeEnd=textObj.selectionEnd;
			var tempStr1=textObj.value.substring(0,rangeStart);
			var tempStr2=textObj.value.substring(rangeEnd);
			textObj.value=tempStr1+textFeildValue+tempStr2;
			textObj.focus();
			var len=textFeildValue.length;
			textObj.setSelectionRange(rangeStart+len,rangeStart+len);
			textObj.blur();
		}else{
			textObj.value+=textFeildValue;
		}
	}
});
